package middleware

import (
	"bp/conf"
	"bp/internal/util"
	"context"
	"go.uber.org/zap"
	"net/http"
)

// WithPager 设置pager
func WithPager(slog *zap.SugaredLogger, vars *conf.VarsSection) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			pageSizeStr := r.URL.Query().Get(util.PageSizeKey)
			pageNoStr := r.URL.Query().Get(util.PageNoKey)
			tsStr := r.URL.Query().Get(util.TsKey)
			pagerPtr := util.ParsePager(pageSizeStr, pageNoStr, tsStr)
			ctx := context.WithValue(r.Context(), util.PagerKey{}, pagerPtr)
			r = r.WithContext(ctx)
			next.ServeHTTP(w, r)
		})
	}
}
